home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / devel / vbcc-src / declaration.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  50KB  |  1,529 lines

  1. /*  $VER: vbcc (declaration.c) V0.4     */
  2.  
  3. #include "vbc.h"
  4.  
  5. static char FILE_[]=__FILE__;
  6.  
  7. #define PARAMETER 8
  8. #define OLDSTYLE 16
  9.  
  10. struct const_list *initialization(struct Typ *,int,int);
  11. int test_assignment(struct Typ *,np);
  12. int return_sc,return_reg,has_return;
  13. #ifdef HAVE_TARGET_ATTRIBUTES
  14. unsigned long return_tattr;
  15. #endif
  16.  
  17. extern int float_used;
  18. extern void optimize(long,struct Var *);
  19.  
  20. int settyp(int typnew, int typold)
  21. /* Unterroutine fuer declaration_specifiers().              */
  22. {
  23.   static int warned_long_double;
  24.   if(DEBUG&2) printf("settyp: new=%d old=%d\n",typnew,typold);
  25.   if(typold==LONG&&typnew==FLOAT){ error(203); return(DOUBLE);}
  26.   if(typold==LONG&&typnew==DOUBLE){
  27.     if(!warned_long_double){error(204);warned_long_double=1;}
  28.     return(DOUBLE);
  29.   }
  30.   if(typold!=0&&typnew!=INT){error(47);return(typnew);}
  31.   if(typold==0&&typnew==INT) return(INT);
  32.   if(typold==0) return(typnew);
  33.   if(typold==SHORT||typold==LONG) return(typold);
  34.   error(48);
  35.   return(typnew);
  36. }
  37.  
  38. #define dsc if(storage_class) error(49); if(typ||type_qualifiers) error(50)
  39. #define XSIGNED 16384
  40.  
  41. struct Typ *declaration_specifiers(void)
  42. /* Erzeugt neuen Typ und gibt Zeiger darauf zurueck,      */
  43. /* parst z.B. unsigned int, struct bla etc.               */
  44. {
  45.   int typ=0,type_qualifiers=0,notdone,storage_class,hard_reg;
  46.   char *merk,*imerk,sident[MAXI],sbuff[MAXI];
  47.   struct Typ *new=mymalloc(TYPS),*t,*ts;
  48.   struct struct_declaration *ssd;
  49.   struct struct_list (*sl)[];
  50.   size_t slsz;
  51.   struct Var *v;
  52. #ifdef HAVE_TARGET_ATTRIBUTES
  53.   unsigned long tattr=0;
  54. #endif
  55.   storage_class=hard_reg=0;
  56.   new->next=0; new->exact=0;
  57.   do{
  58.     killsp();merk=s;cpbez(buff,0);notdone=0;
  59.     if(DEBUG&2) printf("ts: %s\n",buff);
  60.     if(!strcmp("struct",buff)) notdone=STRUCT;
  61.     if(!strcmp("union",buff)) notdone=UNION;
  62.     if(notdone!=0){
  63.       killsp();
  64.       if(*s!='{'){
  65.         cpbez(sident,1);
  66.         killsp();
  67.         ssd=find_struct(sident,0);
  68.         if(ssd&&*s=='{'&&find_struct(sident,nesting)&&ssd->count>0) error(13,sident);
  69.         if(!ssd||((*s=='{'||*s==';')&&!find_struct(sident,nesting))){
  70.           typ=settyp(notdone,typ);
  71.           ssd=mymalloc(sizeof(*ssd));
  72.           ssd->count=0;
  73.           new->exact=ssd=add_sd(ssd);
  74.           add_struct_identifier(sident,ssd);
  75.         }else{
  76.           new->exact=ssd;
  77.           typ=settyp(new->flags=notdone,typ);
  78.         }
  79.       }else{
  80.         *sident=0;
  81.         typ=settyp(notdone,typ);
  82.         ssd=mymalloc(sizeof(*ssd));
  83.         ssd->count=0;
  84.         new->exact=ssd=add_sd(ssd);
  85.       }
  86.       if(*s=='{'){
  87.         s++;
  88.         killsp();
  89.         slsz=SLSIZE;
  90.         sl=mymalloc(slsz*sizeof(struct struct_list));
  91.         ssd->count=0;
  92.         imerk=ident;
  93.         ts=declaration_specifiers();
  94.         while(*s!='}'&&ts){
  95.           ident=sbuff;
  96.           t=declarator(clone_typ(ts));
  97.           killsp();
  98.           if(*s==':'){
  99.             /*  bitfields werden hier noch ignoriert    */
  100.             np tree;
  101.             if((ts->flags&NQ)!=INT) error(51);
  102.             s++;killsp();tree=assignment_expression();
  103.             if(type_expression(tree)){
  104.               if(tree->flags!=CEXPR) error(52);
  105.               if((tree->ntyp->flags&NQ)<CHAR||(tree->ntyp->flags&NQ)>LONG) error(52);
  106.             }
  107.             if(tree) free_expression(tree);
  108.           }else{
  109.             if(*ident==0) error(53);
  110.           }
  111.           if(type_uncomplete(t)){
  112.             error(14,sbuff);
  113.             freetyp(t);
  114.             break;
  115.           }
  116.           if((t->flags&NQ)==FUNKT)
  117.             error(15,sbuff);
  118.  
  119.           if(*ident!=0){
  120.             int i=ssd->count;
  121.             while(--i>=0)
  122.               if(!strcmp((*sl)[i].identifier,ident))
  123.                 error(16,ident);
  124.           }
  125.           (*sl)[ssd->count].styp=t;
  126.           (*sl)[ssd->count].identifier=add_identifier(ident,strlen(ident));
  127.           ssd->count++;
  128.           if(ssd->count>=slsz-1){
  129.             slsz+=SLSIZE;
  130.             sl=realloc(sl,slsz*sizeof(struct struct_list));
  131.             if(!sl){error(12);raus();}
  132.           }
  133.           killsp();
  134.           if(*s==',') {s++;killsp();continue;}
  135.           if(*s!=';') error(54); else s++;
  136.           killsp();
  137.           if(*s!='}'){
  138.             if(ts) freetyp(ts);
  139.             ts=declaration_specifiers();killsp();
  140.           }
  141.         }
  142.         if(ts) freetyp(ts);
  143.         if(ssd->count==0) error(55);
  144.         ident=imerk;
  145.         add_sl(ssd,sl);
  146.         free(sl);
  147.         if(*s!='}') error(56); else s++;
  148.         new->flags=notdone|type_qualifiers;
  149.       }
  150.       notdone=1;
  151.     }
  152.     if(!strcmp("enum",buff)){
  153.       /*  enumerations; die Namen werden leider noch ignoriert    */
  154.       killsp();notdone=1;
  155.       if(*s!='{'){cpbez(buff,1);killsp();}
  156.       if(*s=='{'){
  157.         zlong val; struct Var *v; struct Typ *t;
  158.         val=l2zl(0L);
  159.         s++;killsp();
  160.         while(*s!='}'){
  161.           cpbez(sident,1);killsp();
  162.           if(*sident==0) {error(56);break;}
  163.           t=mymalloc(TYPS);
  164.           t->flags=CONST|INT;
  165.           t->next=0;
  166.           if(find_var(sident,nesting)) error(17,sident);
  167.           v=add_var(sident,t,AUTO,0); /*  AUTO hier klug? */
  168.           if(*s=='='){
  169.             s++;killsp();
  170.             v->clist=initialization(v->vtyp,0,0);
  171.             val=zi2zl(v->clist->val.vint);killsp();
  172.           }else{
  173.             v->clist=mymalloc(CLS);
  174.             v->clist->val.vint=val;
  175.             v->clist->next=v->clist->other=0;
  176.             v->clist->tree=0;
  177.           }
  178.           vlong=l2zl(1L);val=zladd(val,vlong);
  179.           v->vtyp->flags=CONST|ENUM;
  180.           if(*s!='}'&&*s!=',') {error(56);break;}
  181.           if(*s==',') s++;
  182.           killsp();
  183.           if(*s=='}') {s++; break;}
  184.         }
  185.       }
  186.       killsp();
  187.       typ=settyp(INT,typ);*buff=0;
  188.     }
  189.     if(!strcmp("void",buff)) {typ=settyp(VOID,typ);notdone=1;}
  190.     if(!strcmp("char",buff)) {typ=settyp(CHAR,typ);notdone=1;}
  191.     if(!strcmp("short",buff)) {typ=settyp(SHORT,typ);notdone=1;}
  192.     if(!strcmp("int",buff)) {typ=settyp(INT,typ);notdone=1;}
  193.     if(!strcmp("long",buff)) {typ=settyp(LONG,typ);notdone=1;}
  194.     if(!strcmp("float",buff)) {typ=settyp(FLOAT,typ);notdone=1;}
  195.     if(!strcmp("double",buff)) {typ=settyp(DOUBLE,typ);notdone=1;}
  196.     if(!strcmp("const",buff)){
  197.       if(type_qualifiers&CONST) error(58);
  198.       type_qualifiers|=CONST;notdone=1;
  199.     }
  200.     if(!strcmp("volatile",buff)){
  201.       if(type_qualifiers&VOLATILE) error(58);
  202.       type_qualifiers|=VOLATILE;notdone=1;
  203.     }
  204.     if(!strcmp("unsigned",buff)){
  205.       if(type_qualifiers&(XSIGNED|UNSIGNED)) error(58);
  206.       notdone=1;type_qualifiers|=UNSIGNED;
  207.     }
  208.     if(!strcmp("signed",buff)){
  209.       if(type_qualifiers&(XSIGNED|UNSIGNED)) error(58);
  210.       notdone=1;type_qualifiers|=XSIGNED;
  211.     }
  212.     if(!strcmp("auto",buff)) {dsc;storage_class=AUTO;notdone=1;}
  213.     if(!strcmp("register",buff)){dsc;storage_class=REGISTER;notdone=1;}
  214.     if(!strcmp("static",buff)) {dsc;storage_class=STATIC;notdone=1;}
  215.     if(!strcmp("extern",buff)) {dsc;storage_class=EXTERN;notdone=1;}
  216.     if(!strcmp("typedef",buff)) {dsc;storage_class=TYPEDEF;notdone=1;}
  217.     if(!(c_flags[7]&USEDFLAG)&&!strcmp("__reg",buff)){
  218.       char *d;int f=0;
  219.       killsp(); if(*s=='(') s++; else error(151);
  220.       killsp(); if(*s=='\"') s++; else error(74);
  221.       d=buff;
  222.       while(*s&&*s!='\"'){
  223.         if(d-buff-2>MAXI){
  224.           if(!f){ error(206,MAXI);f=1;}
  225.         }else *d++=*s;
  226.         s++;
  227.       }
  228.       *d=0;
  229.       if(*s=='\"') s++; else error(74);
  230.       killsp(); if(*s==')') s++; else error(59);
  231.       for(hard_reg=1;hard_reg<=MAXR;hard_reg++){
  232.         if(!strcmp(buff,regnames[hard_reg])) break;
  233.       }
  234.       if(hard_reg>MAXR){ hard_reg=0;error(220,buff);}
  235.       notdone=1;
  236.     }
  237. #ifdef HAVE_TARGET_ATTRIBUTES
  238.     if(!(c_flags[7]&USEDFLAG)){
  239.       int i;
  240.       for(i=0;g_attr_name[i];i++){
  241.         if(!strcmp(g_attr_name[i],buff)){
  242.           if(tattr&(1L<<i)) error(227,buff);
  243.           tattr|=(1L<<i);
  244.           notdone=1;break;
  245.         }
  246.       }
  247.     }
  248. #endif
  249.  
  250.     if(!notdone&&*buff&&typ==0&&!(type_qualifiers&(XSIGNED|UNSIGNED))){
  251.       v=find_var(buff,0);
  252.       if(v&&v->storage_class==TYPEDEF){
  253.         free(new);
  254.         new=clone_typ(v->vtyp);
  255.         typ=settyp(new->flags,typ);
  256.         notdone=1;
  257.       }
  258.     }
  259.     if(DEBUG&2) printf("typ:%d\n",typ);
  260.   }while(notdone);
  261.   s=merk;killsp();
  262.   return_sc=storage_class;
  263.   return_reg=hard_reg;
  264. #ifdef HAVE_TARGET_ATTRIBUTES
  265.   return_tattr=tattr;
  266. #endif
  267.   if(typ==0){
  268.     if(storage_class==0&&type_qualifiers==0&&!hard_reg) {free(new);return(0);}
  269.     typ=INT;
  270.   }
  271.   if(type_qualifiers&(XSIGNED|UNSIGNED))
  272.     if(typ!=INT&&typ!=CHAR&&typ!=LONG&&typ!=SHORT)
  273.       error(58);
  274.   if(DEBUG&2) printf("ts finish:%s\n",s);
  275.   new->flags=typ|type_qualifiers;
  276.   return(new);
  277. }
  278.  
  279. struct Typ *declarator(struct Typ *a)
  280. /* Erzeugt einen neuen Typ, auf Basis des Typs a.           */
  281. /* a wird hiermit verkettet.                                */
  282. {
  283.   struct Typ *t;
  284.   killsp();*ident=0;
  285.   t=direct_declarator(pointer(a));
  286.   if(!a) {if(t) freetyp(t);return(0);} else return(t);
  287. }
  288. struct Typ *pointer(struct Typ *a)
  289. /* Unterroutine fuer declaration(), behandelt Zeiger auf Typ.   */
  290. {
  291.   struct Typ *t;char *merk;int notdone;
  292.   if(!a) return(0);
  293.   killsp();
  294.   while(*s=='*'){
  295.     s++;
  296.     t=mymalloc(TYPS);
  297.     t->flags=POINTER;
  298.     t->next=a;
  299.     a=t;
  300.     do{
  301.       killsp();
  302.       merk=s;cpbez(buff,0);
  303.       notdone=0;
  304.       if(!strcmp("const",buff)) {a->flags|=CONST;notdone=1;}
  305.       if(!strcmp("volatile",buff)) {a->flags|=VOLATILE;notdone=1;}
  306.     }while(notdone);
  307.     s=merk;
  308.   }
  309.   return(a);
  310. }
  311.  
  312. struct Typ *direct_declarator(struct Typ *a)
  313. /*  Unterroutine zu declarator()                    */
  314. /*  behandelt [],(funkt),(dekl).                     */
  315. {
  316.   struct Typ *rek=0,*merk,*p,*t,*first,*last=0;
  317.   struct struct_declaration *fsd;
  318.   struct struct_list (*sl)[];
  319.   size_t slsz;
  320.   char *imerk,fbuff[MAXI];
  321.   killsp();
  322.   if(!isalpha((unsigned char)*s)&&*s!='_'&&*s!='('&&*s!='[') return(a);
  323.   if(isalpha((unsigned char)*s)||*s=='_'){
  324.     cpbez(ident,1);
  325.     if(!a) return(0);
  326.   }else if(*s=='('&&a){
  327.     /* Rekursion */
  328.     imerk=s; s++; killsp();
  329.     if(*s!=')'&&*ident==0&&!declaration(0)){
  330.       merk=a;
  331.       rek=declarator(a);
  332.       if(*s!=')') error(59); else s++;
  333.     }else s=imerk;
  334.   }
  335.   if(!a)return(0);
  336.   killsp();
  337.   while(*s=='['||*s=='('){
  338.     if(*s=='['){
  339.       s++;
  340.       killsp();
  341.       p=mymalloc(TYPS);
  342.       p->flags=ARRAY;
  343.       p->next=0;
  344.       if(*s==']'){
  345.         p->size=l2zl(0L);
  346.       }else{
  347.         np tree;
  348.         tree=expression();
  349.         if(!type_expression(tree)){
  350.           /*                    error("incorrect constant expression");*/
  351.         }else{
  352.           if(tree->sidefx) error(60);
  353.           if(tree->flags!=CEXPR||(tree->ntyp->flags&NQ)<CHAR||(tree->ntyp->flags&NQ)>LONG){
  354.             error(19);
  355.           }else{
  356.             eval_constn(tree);
  357.             p->size=vlong;
  358.             if(zleqto(p->size,l2zl(0L))) {error(61);p->size=l2zl(1L);}
  359.           }
  360.         }
  361.         free_expression(tree);
  362.       }
  363.       if(*s!=']') error(62); else s++;
  364.       if(last){
  365.         last->next=p;
  366.         last=p;
  367.       }else{
  368.         first=last=p;
  369.       }
  370.     }
  371.     if(*s=='('){
  372.       int komma,oldstyle=0;
  373. #ifdef HAVE_REGPARMS
  374.       struct reg_handle reg_handle=empty_reg_handle;
  375.       struct Typ rpointer={POINTER};
  376.       if(!freturn(a)){
  377.         rpointer.next=a;
  378.         if(!reg_parm(®_handle,&rpointer,0)) ierror(0);
  379.       }
  380. #endif
  381.       s++;
  382.       killsp();
  383.       fsd=mymalloc(sizeof(*fsd));
  384.       slsz=SLSIZE;
  385.       sl=mymalloc(sizeof(struct struct_list)*slsz);
  386.       fsd->count=0;
  387.       imerk=ident;komma=0;
  388.       enter_block();
  389.       while(*s!=')'&&*s!='.'){
  390.         int hard_reg;
  391.         ident=fbuff;*fbuff=0;komma=0;
  392.         t=declaration_specifiers();
  393.         hard_reg=return_reg;
  394.         t=declarator(t);
  395.         if(!t){
  396.           oldstyle=1;
  397.           if(*ident==0) {error(20);break;}
  398.         }
  399.         if(fsd->count){
  400.           if((t&&!(*sl)[fsd->count-1].styp)||
  401.              (!t&&(*sl)[fsd->count-1].styp))
  402.             error(63);
  403.         }
  404.         if(!return_sc) return_sc=AUTO;
  405.         if(return_sc!=AUTO&&return_sc!=REGISTER)
  406.           {error(21);return_sc=AUTO;}
  407.         (*sl)[fsd->count].styp=t;
  408.         (*sl)[fsd->count].storage_class=return_sc;
  409.         if(hard_reg&&!regok(hard_reg,t->flags,0)) error(217,regnames[hard_reg]);
  410.         (*sl)[fsd->count].identifier=add_identifier(ident,strlen(ident));
  411.         if(t){
  412.           if(((*sl)[fsd->count].styp->flags&NQ)==VOID&&fsd->count!=0)
  413.             error(22);
  414.           /*  Arrays in Zeiger umwandeln  */
  415.           if(((*sl)[fsd->count].styp->flags&NQ)==ARRAY)
  416.             (*sl)[fsd->count].styp->flags=POINTER;
  417.           /*  Funktionen in Zeiger auf Funktionen umwandeln   */
  418.           if(((*sl)[fsd->count].styp->flags&NQ)==FUNKT){
  419.             struct Typ *new;
  420.             new=mymalloc(TYPS);
  421.             new->flags=POINTER;
  422.             new->next=(*sl)[fsd->count].styp;
  423.             (*sl)[fsd->count].styp=new;
  424.           }
  425.         }
  426. #ifdef HAVE_REGPARMS
  427.         if(t) (*sl)[fsd->count].reg=reg_parm(®_handle,t,0);
  428.         if(hard_reg) (*sl)[fsd->count].reg=hard_reg;
  429. #else
  430.         (*sl)[fsd->count].reg=hard_reg;
  431. #endif
  432.         fsd->count++;
  433.         if(fsd->count>=slsz-2){     /*  eins Reserve fuer VOID  */
  434.           slsz+=SLSIZE;
  435.           sl=realloc(sl,slsz*sizeof(struct struct_list));
  436.           if(!sl){error(12);raus();}
  437.         }
  438.         killsp(); /* Hier Syntaxpruefung strenger machen */
  439.         if(*s==',') {s++;komma=1; killsp();}
  440.       }
  441.       ident=imerk;
  442.       if((*s!='.'||*(s+1)!='.'||*(s+2)!='.')||!komma){
  443.         if(fsd->count>0&&(!(*sl)[fsd->count-1].styp||((*sl)[fsd->count-1].styp->flags&NQ)!=VOID)){
  444.           (*sl)[fsd->count].styp=mymalloc(TYPS);
  445.           (*sl)[fsd->count].styp->flags=VOID;
  446.           (*sl)[fsd->count].styp->next=0;
  447.           (*sl)[fsd->count].identifier=empty;
  448.           fsd->count++;
  449.         }
  450.       }else if(komma){
  451.         s+=3;komma=0;
  452.         if(oldstyle) error(221);
  453.       }
  454.       p=mymalloc(TYPS);
  455.       p->flags=FUNKT;
  456.       p->next=0;
  457.       {
  458.         int m=nesting;
  459.         nesting=0;
  460.         p->exact=add_sd(fsd);
  461.         add_sl(fsd,sl);
  462.         free(sl);
  463.         nesting=m;
  464.       }
  465.       killsp();
  466.       if(komma) error(59);
  467.       if(*s!=')') error(59); else s++;
  468.       killsp();
  469.       if(*s==','||*s==';'||*s==')'||*s=='=') leave_block();
  470.       if(last){
  471.         last->next=p;
  472.         last=p;
  473.       }else{
  474.         first=last=p;
  475.       }
  476.     }
  477.     killsp();
  478.   }
  479.   if(last){last->next=a;last=a;a=first;}
  480.   if(rek!=0&&rek!=merk){
  481.     /* Zweite Liste anhaengen */
  482.     p=rek;
  483.     while(p->next!=merk) p=p->next;
  484.     if(p) p->next=a; else ierror(0);
  485.     return(rek);
  486.   }
  487.   return(a);
  488. }
  489. int declaration(int offset)
  490. /*  Testet, ob eine Typangabe kommt. Wenn offset!=0 ist,    */
  491. /*  muss s auf '(' zeigen und es wird getestet, ob nach der */
  492. /*  Klammer eine Typangabe kommt.                           */
  493. /*  In jedem Fall zeigt s danach wieder auf dieselbe Stelle */
  494. /*  im Source.                                              */
  495. {
  496.   char *merk=s,buff[MAXI];
  497.   struct Var *v;
  498.   if(offset){
  499.     s++;
  500.     read_new_line=0;
  501.     if(DEBUG&1) printf("cleared read_new_line\n");
  502.     killsp();
  503.     if(read_new_line){  /*  es kam eine neue Zeile  */
  504.       memmove(s+1,s,MAXINPUT);
  505.       *s='(';
  506.       if(DEBUG&1) printf("look-ahead: %s|\n",s);
  507.       merk=s;
  508.       s++;
  509.       cpbez(buff,0);
  510.     }else{
  511.       if(DEBUG&1) printf("read_new_line unchanged\n");
  512.       cpbez(buff,0);
  513.     }
  514.   }else{
  515.     cpbez(buff,0);
  516.   }
  517.   s=merk;
  518.   if(!strcmp("auto",buff)) return(1);
  519.   if(!strcmp("char",buff)) return(1);
  520.   if(!strcmp("const",buff)) return(1);
  521.   if(!strcmp("double",buff)) return(1);
  522.   if(!strcmp("enum",buff)) return(1);
  523.   if(!strcmp("extern",buff)) return(1);
  524.   if(!strcmp("float",buff)) return(1);
  525.   if(!strcmp("int",buff)) return(1);
  526.   if(!strcmp("long",buff)) return(1);
  527.   if(!strcmp("register",buff)) return(1);
  528.   if(!strcmp("short",buff)) return(1);
  529.   if(!strcmp("signed",buff)) return(1);
  530.   if(!strcmp("static",buff)) return(1);
  531.   if(!strcmp("struct",buff)) return(1);
  532.   if(!strcmp("typedef",buff)) return(1);
  533.   if(!strcmp("union",buff)) return(1);
  534.   if(!strcmp("unsigned",buff)) return(1);
  535.   if(!strcmp("void",buff)) return(1);
  536.   if(!strcmp("volatile",buff)) return(1);
  537.   if(!(c_flags[7]&USEDFLAG)&&!strcmp("__reg",buff)) return(1);
  538.   v=find_var(buff,0);
  539.   if(v&&v->storage_class==TYPEDEF) return(1);
  540.   return(0);
  541. }
  542. void add_sl(struct struct_declaration *sd,struct struct_list (*sl)[])
  543. /*  Fuegt ein struct_list-Array in eine struct_declaration ein.     */
  544. /*  Das Array muss mind. sd->count Elements haben und wird kopiert. */
  545. {
  546.   size_t sz=sizeof(struct struct_list)*sd->count;
  547.   sd->sl=mymalloc(sz);
  548.   memcpy(sd->sl,sl,sz);
  549. }
  550. struct struct_declaration *add_sd(struct struct_declaration *new)
  551. /*  Fuegt eine struct Declaration in Liste ein.     */
  552. {
  553.   new->next=0;
  554.   if(first_sd[nesting]==0){
  555.     first_sd[nesting]=last_sd[nesting]=new;
  556.   }else{
  557.     last_sd[nesting]->next=new;
  558.     last_sd[nesting]=new;
  559.   }
  560.   return(new);
  561. }
  562. void free_sd(struct struct_declaration *p)
  563. /*  Gibt eine struct_declaration-List inkl. struct_lists und    */
  564. /*  allen Typen jeder struct_list frei, nicht aber identifier.  */
  565. {
  566.   int i;struct struct_declaration *merk;
  567.   while(p){
  568.     merk=p->next;
  569.     for(i=0;i<p->count;i++) if((*p->sl)[i].styp) freetyp((*p->sl)[i].styp);
  570.     if(p->count>0) free(p->sl);
  571.     free(p);
  572.     p=merk;
  573.   }
  574. }
  575. char *add_identifier(char *identifier,int length)
  576. /*  Kopiert identifier an sicheren Ort, der spaeter zentral     */
  577. /*  freigegeben werden kann.                                    */
  578. /*  Sollte noch einbauen, dass ueberprueft wird, ob schon       */
  579. /*  vorhanden und dann nicht zweimal speichern.                 */
  580. {
  581.   struct identifier_list *new;
  582.   if((*identifier==0&&length==0)||identifier==empty) return(empty);
  583.   new=mymalloc(sizeof(struct identifier_list));
  584.   new->identifier=mymalloc(length+1);
  585.   memcpy(new->identifier,identifier,length+1);
  586.   new->next=0;new->length=length;
  587.   if(last_ilist[nesting]){
  588.     last_ilist[nesting]->next=new;
  589.     last_ilist[nesting]=new;
  590.   }else{
  591.     last_ilist[nesting]=first_ilist[nesting]=new;
  592.   }
  593.   return(new->identifier);
  594. }
  595. void free_ilist(struct identifier_list *p)
  596. /*  Gibt eine verkettete identifier_liste und saemtliche darin  */
  597. /*  gespeicherten Identifier frei.                              */
  598. {
  599.   struct identifier_list *merk;
  600.   while(p){
  601.     merk=p->next;
  602.     if(p->identifier) free(p->identifier);
  603.     free(p);
  604.     p=merk;
  605.   }
  606. }
  607. int type_uncomplete(struct Typ *p)
  608. /*  Testet, ob Typ unvollstaendig ist. Momentan gelten nur      */
  609. /*  unvollstaendige Strukturen und Arrays von solchen als       */
  610. /*  unvollstaendig, aber keine Zeiger oder Funktionen darauf.   */
  611. {
  612.   struct struct_declaration *sd;
  613.   if(!p){ierror(0);return(0);}
  614.   if((p->flags&NQ)==STRUCT||(p->flags&NQ)==UNION)
  615.     if(p->exact->count<=0) return(1);
  616.   if((p->flags&NQ)==ARRAY){
  617.     if(zlleq(p->size,l2zl(0L))) return(1);
  618.     if(type_uncomplete(p->next)) return(1);
  619.   }
  620.   return(0);
  621. }
  622. void add_struct_identifier(char *identifier,struct struct_declaration *sd)
  623. /*  Erzeugt neuen struct_identifier, fuegt ihn in Liste an und  */
  624. /*  vervollstaendigt unvollstaendige Typen dieser Struktur.     */
  625. {
  626.   struct struct_identifier *new;
  627. /*    struct Typ *t;*/
  628.   if(DEBUG&1) printf("add_si %s (nesting=%d)->%p\n",identifier,nesting,(void *)sd);
  629.   new=mymalloc(sizeof(struct struct_identifier));
  630.   new->identifier=add_identifier(identifier,strlen(identifier));
  631.   new->sd=sd; new->next=0;
  632.   if(first_si[nesting]==0){
  633.     first_si[nesting]=new;last_si[nesting]=new;
  634.   }else{
  635.     last_si[nesting]->next=new;last_si[nesting]=new;
  636.   }
  637. }
  638. void free_si(struct struct_identifier *p)
  639. /*  Gibt eine struct_identifier-Liste frei, aber nicht die      */
  640. /*  identifiers und struct_declarations.                        */
  641. {
  642.   struct struct_identifier *merk;
  643.   while(p){
  644.     merk=p->next;
  645.     free(p);
  646.     p=merk;
  647.   }
  648. }
  649. struct struct_declaration *find_struct(char *identifier,int endnesting)
  650. /*  Sucht angegebene Strukturdefinition und liefert             */
  651. /*  entsprechende struct_declaration.                           */
  652. {
  653.   struct struct_identifier *si; int i;
  654.   for(i=nesting;i>=endnesting;i--){
  655.     si=first_si[i];
  656.     while(si){
  657.       if(!strcmp(si->identifier,identifier)){
  658.         if(DEBUG&1) printf("found struct tag <%s> at nesting %d->%p\n",identifier,i,(void *)si->sd);
  659.         return(si->sd);
  660.       }
  661.       si=si->next;
  662.     }
  663.   }
  664.   if(DEBUG&1) printf("didn't find struct tag <%s>\n",identifier);
  665.   return(0);
  666. }
  667. struct Var *add_tmp_var(struct Typ *t)
  668. {
  669.   return add_var(empty,t,AUTO,0);
  670. }
  671. struct Var *add_var(char *identifier, struct Typ *t, int storage_class,struct const_list *clist)
  672. /*  Fuegt eine Variable mit Typ in die var_list ein.            */
  673. /*  In der storage_class werden die Flags PARAMETER und evtl.   */
  674. /*  OLDSTYLE und REGPARM erkannt.                               */
  675. {
  676.   struct Var *new;int f;
  677.   struct struct_declaration *sd;
  678.   static zlong paroffset;
  679.   zlong al;
  680.   /*if(*identifier==0) return;*/ /* sollte woanders bemaekelt werden */
  681.   if(DEBUG&2) printf("add_var(): %s\n",identifier);
  682.   if((t->flags&NQ)==FUNKT&&((t->next->flags&NQ)==ARRAY||(t->next->flags&NQ)==FUNKT))
  683.     error(25);
  684.   new=mymalloc(sizeof(struct Var));
  685.   new->identifier=add_identifier(identifier,strlen(identifier));
  686.   new->clist=clist;
  687.   new->vtyp=t;
  688.   new->storage_class=storage_class&7;
  689.   new->reg=0;
  690.   new->next=0;
  691.   new->flags=0;
  692.   new->fi=0;
  693.   new->nesting=nesting;
  694.   /*    if((storage_class&7)==STATIC||(storage_class&7)==EXTERN) new->flags=USEDASSOURCE|USEDASDEST;*/
  695.   if(DEBUG&2) printf("storage_class=%d\n",storage_class);
  696.   if(storage_class&PARAMETER) new->flags|=USEDASDEST;
  697.   if(storage_class®PARM){
  698.     new->flags|=REGPARM;
  699.     if(!(storage_class&DBLPUSH))
  700.       storage_class&=~PARAMETER;
  701.   }
  702.   if(DEBUG&2) printf("storage_class=%d\n",storage_class);
  703.   if(DEBUG&2) printf("max_offset=%ld\n",max_offset);
  704.   if((storage_class&7)==REGISTER) new->priority=registerpri; else new->priority=0;
  705.   if(last_var[nesting]){
  706.     new->offset=zladd(last_var[nesting]->offset,szof(last_var[nesting]->vtyp));
  707.     last_var[nesting]->next=new;
  708.     last_var[nesting]=new;
  709.   }else{
  710.     new->offset=l2zl(0L);
  711.     paroffset=l2zl(0L);;
  712.     first_var[nesting]=last_var[nesting]=new;
  713.     if(nesting==0) vl1=new;
  714.     if(nesting==1) vl2=new;
  715.   }
  716.   f=t->flags&NQ;
  717.   if((storage_class&7)==AUTO||(storage_class&7)==REGISTER){
  718.     if(DEBUG&2) printf("auto\n");
  719.     if(type_uncomplete(t)&&(t->flags&NQ)!=ARRAY) error(202,identifier);
  720.     /*  das noch ueberpruefen   */
  721.     if((c_flags_val[0].l&2)&&nesting==1&&!(storage_class&PARAMETER)){
  722.       new->offset=max_offset;
  723.     }else{
  724.       if(storage_class&PARAMETER){
  725.         new->offset=paroffset;
  726.       }else{
  727.         new->offset=local_offset[nesting];
  728.       }
  729.     }
  730.     al=falign(t);
  731.     new->offset=zlmult(zldiv(zladd(new->offset,zlsub(al,l2zl(1L))),al),al);
  732.     if(storage_class&PARAMETER){
  733.       new->offset=zlmult(zldiv(zladd(new->offset,zlsub(maxalign,l2zl(1L))),maxalign),maxalign);
  734.       if(f>=CHAR&&f<=SHORT){
  735.         /*  Integer-Erweiterungen fuer alle Funktionsparameter  */
  736.         paroffset=zladd(new->offset,sizetab[INT]);
  737.       }else{
  738.         if(f==FLOAT&&(storage_class&OLDSTYLE)){
  739.           /*  Bei alten Funktionen werden FLOAT als DOUBLE uebergeben */
  740.           new->offset=zlmult(zldiv(zladd(new->offset,zlsub(align[DOUBLE],l2zl(1L))),align[DOUBLE]),align[DOUBLE]);
  741.           paroffset=zladd(new->offset,sizetab[DOUBLE]);
  742.         }else{
  743.           paroffset=zladd(new->offset,szof(new->vtyp));
  744.         }
  745.       }
  746.     }else{
  747.       local_offset[nesting]=zladd(new->offset,szof(new->vtyp));
  748.     }
  749.  
  750.     if(!(storage_class&PARAMETER))
  751.       if(zlleq(max_offset,local_offset[nesting])) max_offset=local_offset[nesting];
  752.     if(DEBUG&2) printf("max_offset=%ld\n",max_offset);
  753.   }
  754.   if((storage_class&7)==STATIC) new->offset=l2zl((long)++label);
  755.   if(storage_class&PARAMETER){
  756.  
  757.     if(DEBUG&2) printf("parameter\n");
  758.  
  759.     if(f>=CHAR&&f<=SHORT&&!zlleq(sizetab[INT],sizetab[f])){
  760.       if(BIGENDIAN){
  761.         new->offset=zladd(new->offset,zlsub(sizetab[INT],sizetab[f]));
  762.       }else{
  763.         if(!LITTLEENDIAN)
  764.           ierror(0);
  765.       }
  766.     }
  767.     if((storage_class&OLDSTYLE)&&f==FLOAT){
  768.       /*  Bei alten Funktionen werden DOUBLE nach FLOAT konvertiert   */
  769.       struct IC *conv=mymalloc(ICS);
  770.       conv->code=CONVDOUBLE;
  771.       conv->typf=FLOAT;
  772.       conv->q1.flags=VAR|DONTREGISTERIZE;
  773.       conv->z.flags=VAR;
  774.       conv->q2.flags=0;
  775.       conv->q1.v=conv->z.v=new;
  776.       conv->q1.val.vlong=conv->z.val.vlong=l2zl(0);
  777.       add_IC(conv);
  778.       new->flags|=CONVPARAMETER;
  779.     }
  780.     new->offset=zlsub(l2zl(0L),zladd(maxalign,new->offset));
  781.   }
  782.   if((storage_class&7)==EXTERN){
  783.     if(!strcmp("fprintf",identifier)) new->flags|=PRINTFLIKE;
  784.     if(!strcmp("printf",identifier))  new->flags|=PRINTFLIKE;
  785.     if(!strcmp("sprintf",identifier)) new->flags|=PRINTFLIKE;
  786.     if(!strcmp("fscanf",identifier))  new->flags|=SCANFLIKE;
  787.     if(!strcmp("scanf",identifier))   new->flags|=SCANFLIKE;
  788.     if(!strcmp("sscanf",identifier))  new->flags|=SCANFLIKE;
  789.   }
  790.   return(new);
  791. }
  792. void free_var(struct Var *p)
  793. /*  Gibt Variablenliste inkl. Typ, aber ohne Identifier frei.   */
  794. {
  795.   struct Var *merk;
  796.   while(p){
  797.     merk=p->next;
  798.     if(!(p->flags&USEDASADR)&&(p->storage_class==AUTO||p->storage_class==REGISTER)){
  799.       if(*p->identifier&&!(p->flags&USEDASDEST)&&(p->vtyp->flags&NQ)<=POINTER) error(64,p->identifier);
  800.       if(*p->identifier&&!(p->flags&USEDASSOURCE)&&(p->vtyp->flags&NQ)<=POINTER) error(65,p->identifier);
  801.     }
  802.     if(DEBUG&2) printf("free_var %s, pri=%d\n",p->identifier,p->priority);
  803.     if(p->vtyp) freetyp(p->vtyp);
  804.     if(p->clist) free_clist(p->clist);
  805.     if(p->fi){
  806.       if(DEBUG&2) printf("free_fi of function %s\n",p->identifier);
  807.       free_fi(p->fi);
  808.       if(DEBUG&2) printf("end free_fi of function %s\n",p->identifier);
  809.     }
  810.     free(p);
  811.     p=merk;
  812.   }
  813. }
  814. struct Var *find_var(char *identifier,int endnesting)
  815. /*  Sucht Variable mit Bezeichner und liefert Zeiger zurueck    */
  816. /*  es werden nur Variablen der Bloecke endnesting-nesting      */
  817. /*  durchsucht.                                                 */
  818. {
  819.   int i;struct Var *v;
  820.   if(*identifier==0||identifier==0) return(0);
  821.   for(i=nesting;i>=endnesting;i--){
  822.     v=first_var[i];
  823.     while(v){
  824.       if(!strcmp(v->identifier,identifier)) return(v);
  825.       v=v->next;
  826.     }
  827.   }
  828.   return(0);
  829. }
  830. void var_declaration(void)
  831. /*  Bearbeitet eine Variablendeklaration und erzeugt alle       */
  832. /*  noetigen Strukturen.                                        */
  833. {
  834.   struct Typ *ts,*t,*old=0,*om=0;char *imerk,vident[MAXI];
  835.   int mdef=0,makeint=0,notdone,storage_class,msc,extern_flag,isfunc,
  836.     had_decl,hard_reg,mhr;
  837.   struct Var *v;
  838. #ifdef HAVE_TARGET_ATTRIBUTES
  839.   unsigned long tattr;
  840. #endif
  841.   ts=declaration_specifiers();notdone=1;
  842.   storage_class=return_sc;hard_reg=return_reg;
  843. #ifdef HAVE_TARGET_ATTRIBUTES
  844.   tattr=return_tattr;
  845. #endif
  846.   if(storage_class==EXTERN) extern_flag=1; else extern_flag=0;
  847.   killsp();
  848.   if(*s==';'){
  849.     if(storage_class||((ts->flags&NQ)!=STRUCT&&(ts->flags&NQ)!=UNION&&(ts->flags&NQ)!=INT))
  850.       error(36);
  851.     freetyp(ts);s++;killsp();
  852.     return;
  853.   }
  854.   if(nesting==0&&(storage_class==AUTO||storage_class==REGISTER))
  855.     {error(66);storage_class=EXTERN;}
  856.   if(!ts){
  857.     if(nesting<=1){
  858.       ts=mymalloc(TYPS);
  859.       ts->flags=INT;ts->next=0;
  860.       makeint=1;
  861.       if(!storage_class) storage_class=EXTERN;
  862.       error(67);
  863.     }else{
  864.       ierror(0);return;
  865.     }
  866.   }
  867.   if(storage_class==0){
  868.     if(nesting==0) storage_class=EXTERN; else storage_class=AUTO;
  869.   }
  870.   msc=storage_class;mhr=hard_reg;
  871.   while(notdone){
  872.     int oldnesting=nesting;
  873.     imerk=ident;ident=vident;*vident=0;  /* merken von ident hier vermutlich */
  874.     storage_class=msc;hard_reg=mhr;
  875.     if(old) {freetyp(old);old=0;}
  876.     t=declarator(clone_typ(ts));
  877.     if((t->flags&NQ)!=FUNKT){
  878.         isfunc=0;
  879.     }else{
  880.         isfunc=1;
  881.         if(storage_class!=STATIC&&storage_class!=TYPEDEF) storage_class=EXTERN;
  882.     }
  883.     ident=imerk;                    /* nicht unbedingt noetig ?         */
  884.     if(!*vident){
  885.       free(ts);free(t);
  886.       error(36);return;
  887.     }
  888.     v=find_var(vident,oldnesting);
  889.     if(v){
  890.       had_decl=1;
  891.       if(storage_class==TYPEDEF){
  892.         error(226,v->identifier);
  893.       }else{
  894.         if(nesting>0&&(v->flags&DEFINED)&&!extern_flag&&!isfunc){
  895.           error(27,vident);
  896.         }else{
  897.           if(t&&v->vtyp&&!compare_pointers(v->vtyp,t,255)){
  898.             error(68,vident);
  899.           }
  900.           if((storage_class!=v->storage_class&&!extern_flag)||hard_reg!=v->reg)
  901.             error(28,v->identifier);
  902.           if(!isfunc&&!extern_flag) v->flags|=TENTATIVE;
  903.         }
  904. #ifdef HAVE_TARGET_ATTRIBUTES
  905.         {
  906.           int i;
  907.           for(i=0;g_attr_name[i];i++){
  908.             if((v->tattr&(1L<<i))!=(tattr&(1L<<i))) error(228,vident,g_attr_name[i]);
  909.           }
  910.           v->tattr=tattr;
  911.         }
  912. #endif
  913.         if(!isfunc){
  914.           v->vtyp=t;
  915.         }else{
  916.           om=v->vtyp;
  917.           if(t->exact->count>0) {old=v->vtyp;v->vtyp=t;}
  918.         }
  919.       }
  920.     }else{
  921.       had_decl=0;
  922.       if(isfunc&&*s!=','&&*s!=';'&&*s!=')'&&*s!='='&&nesting>0) nesting--;
  923.       v=add_var(vident,t,storage_class,0);
  924.       v->reg=hard_reg;
  925. #ifdef HAVE_TARGET_ATTRIBUTES
  926.       v->tattr=tattr;
  927. #endif
  928.       if(isfunc&&*s!=','&&*s!=';'&&*s!=')'&&*s!='='&&nesting>=0) nesting++;
  929.       if(!v) ierror(0);
  930.       else{
  931.         if(!isfunc&&!extern_flag){
  932.           v->flags|=TENTATIVE;
  933.           if(nesting>0) v->flags|=DEFINED;
  934.         }
  935.       }
  936.       om=0;
  937.     }
  938.     killsp();
  939.     /*  Inline-Assembler-Code in Funktionsdeklarationen */
  940.     if(*s=='='&&(v->vtyp->flags&NQ)==FUNKT&&!(c_flags[7]&USEDFLAG)){
  941.       np tree;
  942.       s++;killsp();
  943.       tree=string_expression();
  944.       if(!tree||tree->flags!=STRING) error(42);
  945.       else{
  946.         int l;struct const_list *cl;
  947.         if(!v->fi) v->fi=new_fi();
  948.         cl=tree->cl;l=0;
  949.         while(cl){
  950.           l++;
  951.           cl=cl->next;
  952.         }
  953.         v->fi->inline_asm=mymalloc(l);
  954.         cl=tree->cl;l=0;
  955.         while(cl){
  956.           v->fi->inline_asm[l]=zl2l(zc2zl(cl->other->val.vchar));
  957.           l++;
  958.           cl=cl->next;
  959.         }
  960.       }
  961.       if(tree) free_expression(tree);
  962.       killsp();
  963.     }else{
  964.       if(v->fi){free(v->fi->inline_asm);v->fi->inline_asm=0;}
  965.     }
  966.     /*  Initialisierung von Variablen bei Deklaration   */
  967.     if(*s=='='){
  968.       s++;killsp();
  969.       if(!had_decl&&v->nesting==0&&v->storage_class==EXTERN&&strcmp("main",v->identifier))
  970.         error(168,v->identifier);
  971.       if(v->flags&DEFINED) {if(nesting==0) error(30,v->identifier);}
  972.       else v->flags|=DEFINED;
  973.       if(v->storage_class==TYPEDEF) error(114,v->identifier);
  974.       if(extern_flag){
  975.         if(nesting==0)
  976.           error(118,v->identifier);
  977.         else
  978.           error(207,v->identifier);
  979.         if(v->storage_class!=EXTERN){ error(77);v->storage_class=EXTERN;}
  980.       }
  981.       v->clist=initialization(v->vtyp,v->storage_class==AUTO||v->storage_class==REGISTER,0);
  982.       if(v->clist){
  983.         if((v->vtyp->flags&NQ)==ARRAY&&zleqto(v->vtyp->size,l2zl(0L))){
  984.           struct const_list *p=v->clist;
  985.           while(p){v->vtyp->size=zladd(v->vtyp->size,l2zl(1L));p=p->next;}
  986.           if(v->storage_class==AUTO||v->storage_class==REGISTER){
  987.             local_offset[nesting]=zladd(local_offset[nesting],szof(v->vtyp));
  988.             if(zlleq(max_offset,local_offset[nesting])) max_offset=local_offset[nesting];
  989.           }
  990.         }
  991.         if(v->storage_class==AUTO||v->storage_class==REGISTER){
  992.           struct IC *new;
  993.           /*  Initialisierung von auto-Variablen  */
  994.           new=mymalloc(ICS);
  995.           new->code=ASSIGN;
  996.           new->typf=v->vtyp->flags;
  997.           new->q2.flags=0;
  998.           new->q2.val.vlong=szof(v->vtyp);
  999.           new->z.flags=VAR;
  1000.           new->z.v=v;
  1001.           new->z.val.vlong=l2zl(0L);
  1002.           if(v->clist->tree){
  1003.             /*  einzelner Ausdruck  */
  1004.             gen_IC(v->clist->tree,0,0);
  1005.             convert(v->clist->tree,v->vtyp->flags&NU);
  1006.             new->q1=v->clist->tree->o;
  1007.             /*                        v->clist=0;*/
  1008.           }else{
  1009.             /*  Array etc.  */
  1010.             struct Var *nv;
  1011.             nv=add_var(empty,clone_typ(v->vtyp),STATIC,v->clist);
  1012.             nv->flags|=DEFINED;
  1013.             nv->vtyp->flags|=CONST;
  1014.             /*                        v->clist=0;*/
  1015.             new->q1.flags=VAR;
  1016.             new->q1.v=nv;
  1017.             new->q1.val.vlong=l2zl(0L);
  1018.           }
  1019.           add_IC(new);
  1020.           /*                    if(v->clist&&v->clist->tree){free_expression(v->clist->tree);v->clist->tree=0;}*/
  1021.         }else if(c_flags[19]&USEDFLAG){
  1022.           /*  Ohne Optimierung gleich erzeugen; das ist noch  */
  1023.           /*  etwas von der genauen Implementierung der Liste */
  1024.           /*  der Variablen abhaengig.                        */
  1025.           struct Var *merk=v->next;
  1026.           v->next=0;
  1027.           gen_vars(v);
  1028.           v->next=merk;
  1029.           v->clist=0;
  1030.         }
  1031.       }
  1032.     }else{
  1033.       if((v->flags&DEFINED)&&type_uncomplete(v->vtyp)) error(202,v->identifier);
  1034.       if((v->vtyp->flags&CONST)&&(v->storage_class==AUTO||v->storage_class==REGISTER))
  1035.         error(119,v->identifier);
  1036.     }
  1037.     if(*s==',') {s++;killsp();mdef=1;} else notdone=0;
  1038.   }
  1039.   freetyp(ts);
  1040.   if(!mdef&&t&&(t->flags&NQ)==FUNKT&&*s!=';'){
  1041.     /*  Funktionsdefinition                                     */
  1042.     int i,oldstyle=0;
  1043. #ifdef HAVE_REGPARMS
  1044.     struct reg_handle reg_handle;
  1045. #endif
  1046.     fline=line;
  1047.     if(DEBUG&1) printf("Funktionsdefinition!\n");
  1048.     {int i;
  1049.     for(i=1;i<=MAXR;i++) {regs[i]=regused[i]=regsa[i];regsbuf[i]=0;}
  1050.     }
  1051.     cur_func=v->identifier;
  1052.     if(only_inline==2) only_inline=0;
  1053.     if(nesting<1) ierror(0);
  1054.     if(nesting>1) error(32);
  1055.     if(v->flags&DEFINED) error(33,v->identifier);
  1056.     else v->flags|=DEFINED;
  1057.     if(storage_class!=EXTERN&&storage_class!=STATIC) error(34);
  1058.     if(extern_flag) error(120);
  1059.     if(storage_class==EXTERN&&!strcmp(v->identifier,"main")&&(!t->next||t->next->flags!=INT)) error(121);
  1060.     if(!had_decl&&v->nesting==0&&v->storage_class==EXTERN&&strcmp("main",v->identifier))
  1061.       error(168,v->identifier);
  1062.     while(*s!='{'){
  1063.       /*  alter Stil  */
  1064.       struct Typ *nt=declaration_specifiers();notdone=1;oldstyle=OLDSTYLE;
  1065.       if(!ts) {error(35);}
  1066.       while(notdone){
  1067.         int found=0;
  1068.         imerk=ident;ident=vident;*vident=0;
  1069.         ts=declarator(clone_typ(nt));
  1070.         ident=imerk;
  1071.         if(!ts) {error(36);}
  1072.         else{
  1073.           for(i=0;i<t->exact->count;i++){
  1074.             if(!strcmp((*t->exact->sl)[i].identifier,vident)){
  1075.               found=1;
  1076.               if((*t->exact->sl)[i].styp){
  1077.                 error(69,vident);
  1078.                 freetyp((*t->exact->sl)[i].styp);
  1079.               }
  1080.               /*  typ[] in *typ   */
  1081.               if((ts->flags&NQ)==ARRAY) ts->flags=POINTER;
  1082.               /*  typ() in *typ() */
  1083.               if((ts->flags&NQ)==FUNKT){
  1084.                 struct Typ *new=mymalloc(TYPS);
  1085.                 new->flags=POINTER;
  1086.                 new->next=ts;
  1087.                 ts=new;
  1088.               }
  1089.               if(!return_sc) return_sc=AUTO;
  1090.               if(return_sc!=AUTO&&return_sc!=REGISTER)
  1091.                 {error(122);return_sc=AUTO;}
  1092.               (*t->exact->sl)[i].storage_class=return_sc;
  1093.               (*t->exact->sl)[i].reg=return_reg;
  1094.               if(return_reg) error(219);
  1095.               (*t->exact->sl)[i].styp=ts;
  1096.             }
  1097.           }
  1098.         }
  1099.         if(!found) {error(37,vident);}
  1100.         killsp();
  1101.         if(*s==',') {s++;killsp();} else notdone=0;
  1102.       }
  1103.       if(nt) freetyp(nt);
  1104.       if(*s==';'){s++;killsp();
  1105.       }else{
  1106.         error(54);
  1107.         while(*s!='{'&&*s!=';'){s++;killsp();}
  1108.       }
  1109.     }
  1110.     if(t->exact->count==0){
  1111.       struct struct_list sl[1];
  1112.       if(DEBUG&1) printf("prototype converted to (void)\n");
  1113.       t->exact->count=1;
  1114.       sl[0].identifier=empty;
  1115.       sl[0].storage_class=AUTO;
  1116.       sl[0].styp=mymalloc(TYPS);
  1117.       sl[0].styp->flags=VOID;
  1118.       sl[0].styp->next=0;
  1119.       nesting--;
  1120.       add_sl(t->exact,&sl);
  1121.       nesting++;
  1122.     }
  1123.     if(om&&!compare_sd(om->exact,t->exact))
  1124.       error(123);
  1125.     nocode=0;currentpri=1;
  1126.     /*        enter_block();*/
  1127.     local_offset[1]=l2zl(0L);
  1128.     return_var=0;
  1129.     if(!v->vtyp) ierror(0);
  1130. #ifdef HAVE_REGPARMS
  1131.     reg_handle=empty_reg_handle;
  1132. #endif
  1133.     if(v->vtyp->next->flags==VOID) return_typ=0;
  1134.     else{
  1135.       return_typ=v->vtyp->next;
  1136.       if(!freturn(return_typ)){
  1137.         /*  Parameter fuer die Rueckgabe von Werten, die nicht in einem */
  1138.         /*  Register sind.                                              */
  1139.         struct Typ *rt=mymalloc(TYPS);int reg;
  1140.         rt->flags=POINTER;rt->next=return_typ;
  1141. #ifdef HAVE_REGPARMS
  1142.         reg=reg_parm(®_handle,rt,0);
  1143.         if(!reg) ierror(0);
  1144.         return_var=add_var(empty,clone_typ(rt),reg<0?(AUTO|PARAMETER|REGPARM|DBLPUSH|oldstyle):(AUTO|PARAMETER|REGPARM|oldstyle),0);
  1145.         return_var->reg=reg;
  1146. #else
  1147.         return_var=add_var(empty,clone_typ(rt),AUTO|PARAMETER|oldstyle,0);
  1148. #endif
  1149.         return_var->flags|=DEFINED;
  1150.         free(rt);
  1151.       }
  1152.     }
  1153.     first_ic=last_ic=0;ic_count=0;max_offset=l2zl(0L);
  1154.     for(i=0;i<t->exact->count;i++){
  1155.       if(!(*t->exact->sl)[i].styp&&*(*t->exact->sl)[i].identifier){
  1156.         struct Typ *nt;
  1157.         nt=mymalloc(TYPS);
  1158.         nt->flags=INT; nt->next=0;
  1159.         (*t->exact->sl)[i].styp=nt;
  1160.         (*t->exact->sl)[i].storage_class=AUTO;
  1161.         (*t->exact->sl)[i].reg=0;
  1162.         error(124);
  1163.       }
  1164.       if(*(*t->exact->sl)[i].identifier){
  1165.         struct Var *tmp;int sc,tr;
  1166.         sc=((*t->exact->sl)[i].storage_class|PARAMETER|oldstyle);
  1167. #ifdef HAVE_REGPARMS
  1168.         if(!t->exact->sl) ierror(0);
  1169.         if(!(*t->exact->sl)[i].styp) ierror(0);
  1170.         tr=reg_parm(®_handle,(*t->exact->sl)[i].styp,0);
  1171.         if(!(*t->exact->sl)[i].reg) (*t->exact->sl)[i].reg=tr;
  1172. #endif
  1173.         if((*t->exact->sl)[i].reg>0) sc|=REGPARM;
  1174.         if((*t->exact->sl)[i].reg<0) sc|=(REGPARM|DBLPUSH);
  1175.         tmp=add_var((*t->exact->sl)[i].identifier,clone_typ((*t->exact->sl)[i].styp),sc,0);
  1176.         tmp->reg=(*t->exact->sl)[i].reg;
  1177.         tmp->flags|=DEFINED;
  1178.         if(oldstyle){
  1179.           freetyp((*t->exact->sl)[i].styp);
  1180.           (*t->exact->sl)[i].styp=0; /*  Prototype entfernen */
  1181.         }
  1182.       }
  1183.     }
  1184.     if(oldstyle) t->exact->count=0; /*  Prototype entfernen */
  1185.     /*        local_offset[1]=l2zl(0L);*/
  1186.     return_label=++label;
  1187.     v->flags|=GENERATED;
  1188.     function_calls=0;float_used=0;has_return=0;goto_used=0;
  1189.     compound_statement();
  1190.     if((v->vtyp->next->flags&NQ)!=VOID&&!has_return){
  1191.       if(strcmp(v->identifier,"main")) error(173,v->identifier);
  1192.       else error(174,v->identifier);
  1193.     }
  1194. #if 0
  1195.     {int i;
  1196.     for(i=1;i<=MAXR;i++) if(regs[i]!=regsa[i]) {printf("Register %s:\n",regnames[i]);ierror(0);}
  1197.     }
  1198. #endif
  1199.         gen_label(return_label);
  1200.         if(first_ic&&errors==0){
  1201.           if((c_flags[2]&USEDFLAG)&&ic1){fprintf(ic1,"function %s\n",v->identifier); pric(ic1,first_ic);}
  1202.           vl1=first_var[0];
  1203.           vl2=first_var[1];
  1204.           vl3=merk_varf;
  1205.           optimize(c_flags_val[0].l,v);
  1206.           if((c_flags[3]&USEDFLAG)&&ic2){fprintf(ic2,"function %s\n",v->identifier); pric(ic2,first_ic);}
  1207.           if(out&&!only_inline&&!(c_flags[5]&USEDFLAG)){
  1208.             gen_code(out,first_ic,v,max_offset);
  1209.           }
  1210.           /*            if(DEBUG&8192){fprintf(ic2,"function %s, after gen_code\n",v->identifier); pric(ic2,first_ic);}*/
  1211.           free_IC(first_ic);
  1212.           first_ic=last_ic=0;
  1213.         }
  1214.         if(v->fi&&v->fi->first_ic){
  1215.           struct Var *vp;
  1216.           if(DEBUG&1) printf("leave block %d (inline-version)\n",nesting);
  1217.           if(nesting!=1) ierror(0);
  1218.           if(merk_varl) merk_varl->next=first_var[nesting]; else merk_varf=first_var[nesting];
  1219.           if(last_var[nesting]) merk_varl=last_var[nesting];
  1220.           if(merk_sil) merk_sil->next=first_si[nesting]; else merk_sif=first_si[nesting];
  1221.           if(last_si[nesting]) merk_sil=last_si[nesting];
  1222.           if(merk_sdl) merk_sdl->next=first_sd[nesting]; else merk_sdf=first_sd[nesting];
  1223.           if(last_sd[nesting]) merk_sdl=last_sd[nesting];
  1224.           if(merk_ilistl) merk_ilistl->next=first_ilist[nesting]; else merk_ilistf=first_ilist[nesting];
  1225.           if(last_ilist[nesting]) merk_ilistl=last_ilist[nesting];
  1226.  
  1227.           if(merk_varf&&!only_inline) gen_vars(merk_varf);
  1228.           if(first_llist) free_llist(first_llist);
  1229.           if(first_clist) free_clist(first_clist);
  1230.           if(merk_sif) free_si(merk_sif);
  1231. /*  struct-declarations erst ganz am Schluss loeschen. Um zu vermeiden,     */
  1232. /*  dass struct-declarations in Prototypen frei werden und dann eine        */
  1233. /*  spaetere struct, dieselbe Adresse bekommt und dadurch gleich wird.      */
  1234. /*  Nicht sehr schoen - wenn moeglich noch mal aendern.                     */
  1235. /*            if(merk_sdf) free_sd(merk_sdf);*/
  1236.             /*  hier noch was ueberlegen    */
  1237. /*            if(merk_ilistf) free_ilist(merk_ilistf);*/
  1238.           nesting--;
  1239.           v->fi->vars=merk_varf;
  1240. /*            v->fi->vars=first_var[1];*/
  1241.             /*  keine echten Parameter=>keine negativen Offsets */
  1242. /*            vp=first_var[1];*/
  1243.           vp=merk_varf;
  1244.           while(vp){
  1245.             if(vp->storage_class==AUTO||vp->storage_class==REGISTER){
  1246.               if(!zlleq(l2zl(0L),vp->offset)){
  1247.                 vp->offset=l2zl(0L);
  1248.                 if(DEBUG&1024) printf("converted parameter <%s>(%ld) for inlining\n",vp->identifier,(long)zl2l(vp->offset));
  1249.               }else vp->offset=l2zl(4L);  /*  Dummy, da recalc_offsets?   */
  1250.             }
  1251.             vp=vp->next;
  1252.           }
  1253.         }else{
  1254.           leave_block();
  1255.         }
  1256.         if(only_inline==2) only_inline=0;
  1257.         cur_func="oops, I forgot it";
  1258.   }else{
  1259.     if(makeint) error(125);
  1260.     if(*s==';') s++; else error(54);
  1261.     if((t->flags&NQ)==FUNKT&&t->exact){
  1262.       struct struct_declaration *sd=t->exact;int i,f;
  1263.       for(f=0,i=0;i<sd->count;i++)
  1264.         if(!(*sd->sl)[i].styp){error(126);f=1;}
  1265.       if(f){
  1266.         for(i=0;i<sd->count;i++) if((*sd->sl)[i].styp) freetyp((*sd->sl)[i].styp);
  1267.         sd->count=0;
  1268.       }
  1269.     }
  1270.   }
  1271.   if(old) freetyp(old);
  1272. }
  1273. int compare_pointers(struct Typ *a,struct Typ *b,int qual)
  1274. /*  Vergleicht, ob Typ beider Typen gleich ist, const/volatile      */
  1275. /*  werden laut ANSI nicht beruecksichtigt.                         */
  1276. {
  1277.   struct struct_declaration *sd;
  1278.   int af=a->flags&qual,bf=b->flags&qual;
  1279.   if(af!=bf) return(0);
  1280.   af&=NQ;bf&=NQ;
  1281.   if(af==FUNKT){
  1282.     if(a->exact->count&&!compare_sd(a->exact,b->exact)) return(0);
  1283.   }
  1284.   if(af==STRUCT||af==UNION){
  1285.     if(a->exact!=b->exact) return(0);
  1286.   }
  1287.   if(af==ARRAY){
  1288.     if(!zleqto(a->size,l2zl(0L))&&!zleqto(b->size,l2zl(0L))&&!zleqto(a->size,b->size)) return(0);
  1289.   }
  1290.   if(a->next==0&&b->next!=0) return(0);
  1291.   if(a->next!=0&&b->next==0) return(0);
  1292.   if(a->next==0&&b->next==0) return(1);
  1293.   return(compare_pointers(a->next,b->next,qual));
  1294. }
  1295. int compare_sd(struct struct_declaration *a,struct struct_declaration *b)
  1296. /*  Vergleicht, ob zwei struct_declarations identisch sind          */
  1297. /*  Wird nur nur fuer Prototypen benutzt, leere Liste immer gleich. */
  1298. {
  1299.   int i;
  1300.   if(!a->count||!b->count) return(1);
  1301.   if(a->count!=b->count) return(0);
  1302.   for(i=0;i<a->count;i++)
  1303.     if((*a->sl)[i].styp&&(*b->sl)[i].styp&&!compare_pointers((*a->sl)[i].styp,(*b->sl)[i].styp,255)) return(0);
  1304.   return(1);
  1305. }
  1306. void free_clist(struct const_list *p)
  1307. /*  Gibt clist frei.                                        */
  1308. {
  1309.   struct const_list *merk;
  1310.   return;
  1311.   while(p){
  1312.     merk=p->next;
  1313.     if(p->other) free_clist(p->other);
  1314.     if(p->tree) free_expression(p->tree);
  1315.     free(p);
  1316.     p=merk;
  1317.   }
  1318. }
  1319. void gen_clist(FILE *,struct Typ *,struct const_list *);
  1320.  
  1321. void gen_vars(struct Var *v)
  1322. /*  Generiert Variablen.                                    */
  1323. {
  1324.   int mode,al;struct Var *p;
  1325.   if(errors!=0||(c_flags[5]&USEDFLAG)) return;
  1326.   for(mode=0;mode<3;mode++){
  1327.     int i,flag;
  1328.     for(p=v;p;p=p->next){
  1329.       if(DEBUG&2) printf("gen_var(): %s\n",p->identifier);
  1330.       if(p->storage_class==STATIC||p->storage_class==EXTERN){
  1331.         if(!(p->flags&GENERATED)){
  1332.           if(p->storage_class==EXTERN&&!(p->flags&(USEDASSOURCE|USEDASDEST))&&!(p->flags&(TENTATIVE|DEFINED))) continue;
  1333.           /*  erst konstante initialisierte Daten */
  1334.           if(mode==0){
  1335.             if(!p->clist) continue;
  1336.             if(!(p->vtyp->flags&(CONST|STRINGCONST))){
  1337.               struct Typ *t=p->vtyp;int f=0;
  1338.               do{
  1339.                 if(t->flags&(CONST|STRINGCONST)) break;
  1340.                 if((t->flags&NQ)!=ARRAY){f=1;break;}
  1341.                 t=t->next;
  1342.               }while(1);
  1343.               if(f) continue;
  1344.             }
  1345.           }
  1346.           /*  dann initiolisierte */
  1347.           if(mode==1&&!p->clist) continue;
  1348.           /*  und dann der Rest   */
  1349.           if(mode==2&&p->clist) continue;
  1350.           if(!(p->flags&(TENTATIVE|DEFINED))){
  1351.             gen_var_head(out,p);
  1352.             if(p->storage_class==STATIC) error(127,p->identifier);
  1353.             continue;
  1354.           }else{
  1355.             gen_align(out,falign(p->vtyp));
  1356.           }
  1357.           gen_var_head(out,p);
  1358.           if(!p->clist){
  1359.             if(type_uncomplete(p->vtyp)) error(202,p->identifier);
  1360.             gen_ds(out,szof(p->vtyp),p->vtyp);
  1361.           }else{
  1362.             gen_clist(out,p->vtyp,p->clist);
  1363.           }
  1364.           p->flags|=GENERATED;
  1365.         }
  1366.       }
  1367.     }
  1368.   }
  1369. }
  1370. void gen_clist(FILE *f,struct Typ *t,struct const_list *cl)
  1371. /*  Generiert dc fuer const_list.                           */
  1372. {
  1373.   int i;zlong sz;
  1374.   if((t->flags&NQ)==ARRAY){
  1375.     for(sz=l2zl(0L);!zlleq(t->size,sz)&&cl;sz=zladd(sz,l2zl(1L)),cl=cl->next){
  1376.       if(!cl->other){ierror(0);return;}
  1377.       gen_clist(f,t->next,cl->other);
  1378.     }
  1379.     if(!zlleq(t->size,sz)) gen_ds(f,zlmult(zlsub(t->size,sz),szof(t->next)),t->next);
  1380.     return;
  1381.   }
  1382.   if((t->flags&NQ)==UNION){
  1383.     gen_clist(f,(*t->exact->sl)[0].styp,cl);
  1384.     sz=zlsub(szof(t),szof((*t->exact->sl)[0].styp));
  1385.     if(!zleqto(sz,l2zl(0L))) gen_ds(f,sz,0);
  1386.     return;
  1387.   }
  1388.   if((t->flags&NQ)==STRUCT){
  1389.     zlong al;int fl;struct Typ *st;
  1390.     sz=l2zl(0L);
  1391.     for(i=0;i<t->exact->count&&cl;i++){
  1392.       if(!cl->other){ierror(0);return;}
  1393.       st=(*t->exact->sl)[i].styp;
  1394.       al=falign(st);
  1395.       if(!zleqto(zlmod(sz,al),l2zl(0L))){
  1396.         gen_ds(f,zlsub(al,zlmod(sz,al)),0);
  1397.         sz=zladd(sz,zlsub(al,zlmod(sz,al)));
  1398.       }
  1399.       if(!(*t->exact->sl)[i].identifier) ierror(0);
  1400.       if((*t->exact->sl)[i].identifier[0]){
  1401.         gen_clist(f,st,cl->other);
  1402.         cl=cl->next;
  1403.       }else{
  1404.         gen_ds(f,szof(st),0); /* sollte unnamed bitfield sein */
  1405.       }
  1406.       sz=zladd(sz,szof(st));
  1407.     }
  1408.     for(;i<t->exact->count;i++){
  1409.       st=(*t->exact->sl)[i].styp;
  1410.       al=falign(st);
  1411.       if(!zleqto(zlmod(sz,al),l2zl(0L))){
  1412.         gen_ds(f,zlsub(al,zlmod(sz,al)),0);
  1413.         sz+=zladd(sz,zlsub(al,zlmod(sz,al)));
  1414.       }
  1415.       gen_ds(f,szof((*t->exact->sl)[i].styp),(*t->exact->sl)[i].styp);
  1416.       sz=zladd(sz,szof(st));
  1417.     }
  1418.     al=falign(t);
  1419.     if(!zleqto(zlmod(sz,al),l2zl(0L)))
  1420.       gen_ds(f,zlsub(al,zlmod(sz,al)),0);
  1421.     return;
  1422.   }
  1423.   if(cl->tree) cl->tree->o.am=0;
  1424.   gen_dc(f,t->flags&NU,cl);
  1425. }
  1426. struct const_list *initialization(struct Typ *t,int noconst,int level)
  1427. /*  Traegt eine Initialisierung in eine const_list ein.         */
  1428. {
  1429.   struct const_list *first,*cl,**prev;np tree,tree2;int bracket;zlong i;
  1430.   int f=t->flags&NQ;
  1431.   if(f==FUNKT){error(42);return(0);}
  1432.   if(*s=='{'){s++;killsp();bracket=1;} else bracket=0;
  1433.   if(f==ARRAY){
  1434.     if(*s=='\"'&&t->next&&(t->next->flags&NQ)==CHAR){
  1435.       killsp();
  1436.       tree=string_expression();
  1437.       first=tree->cl;
  1438.       free_expression(tree);
  1439.     }else{
  1440.       prev=0;
  1441.       if(level==0&&!bracket) error(157);
  1442.       for(i=l2zl(0L);(zleqto(t->size,l2zl(0L))||!zlleq(t->size,i))&&*s!='}';i=zladd(i,l2zl(1L))){
  1443.         if(!zlleq(i,0)){
  1444.           if(*s==','){s++;killsp();} else break;
  1445.           if(*s=='}') break;
  1446.         }
  1447.         cl=mymalloc(CLS);
  1448.         cl->next=0;cl->tree=0;
  1449.         cl->other=initialization(t->next,0,level+1);
  1450.         killsp();
  1451.         if(prev) *prev=cl; else first=cl;
  1452.         prev=&cl->next;
  1453.       }
  1454.     }
  1455.   }else if(f==STRUCT&&(bracket||!noconst)){
  1456.     if(t->exact->count<=0)
  1457.       {error(43);return(0);}
  1458.     prev=0;
  1459.     if(level==0&&!bracket) error(157);
  1460.     for(i=l2zl(0L);!zlleq(t->exact->count,i)&&*s!='}';i=zladd(i,l2zl(1L))){
  1461.       if((*t->exact->sl)[zl2l(i)].identifier[0]==0) {continue;} /* unnamed bitfield */
  1462.       if(!zlleq(i,0)){
  1463.         if(*s==','){s++;killsp();} else break;
  1464.         if(*s=='}') break;
  1465.       }
  1466.       cl=mymalloc(CLS);
  1467.       cl->next=0;cl->tree=0;
  1468.       cl->other=initialization((*t->exact->sl)[zl2l(i)].styp,0,level+1);
  1469.       if(prev) *prev=cl; else first=cl;
  1470.       prev=&cl->next;
  1471.     }
  1472.   }else if(f==UNION&&(bracket||!noconst)){
  1473.     if(t->exact->count<=0)
  1474.       {error(44);return(0);}
  1475.     first=initialization((*t->exact->sl)[0].styp,0,level+1);
  1476.   }else{
  1477.     tree2=tree=assignment_expression();
  1478.     if(!tree){error(45);return(0);}
  1479.     if(!type_expression(tree)){free_expression(tree); return(0);}
  1480.     tree=makepointer(tree);
  1481.     test_assignment(t,tree);
  1482.     if(!noconst){
  1483.       /*  nur Konstanten erlaubt (bei Arrays/Strukturen etc. oder static) */
  1484.       if(tree->flags!=CEXPR){
  1485.         while(tree->flags==CAST) tree=tree->left;
  1486.         if(tree->flags==ADDRESS||tree->flags==ADDRESSS||tree->flags==ADDRESSA){
  1487.           gen_IC(tree,0,0);
  1488.           if(!(tree->o.flags&VARADR)){
  1489.             /*  hier fehlen noch viele Pruefungen   */
  1490.             free_expression(tree);error(46);
  1491.             return(0);
  1492.           }
  1493.           first=mymalloc(CLS);
  1494.           first->next=first->other=0;
  1495.           first->tree=tree;
  1496.           killsp();
  1497.         }else{
  1498.           free_expression(tree);error(46);
  1499.           return(0);
  1500.         }
  1501.       }else{
  1502.         first=mymalloc(CLS);
  1503.         first->next=first->other=0;
  1504.         first->tree=0;
  1505.         eval_constn(tree);
  1506.         tree->ntyp->flags=t->flags;
  1507.         insert_const(tree);
  1508.         first->val=tree->val;
  1509.         free_expression(tree2);
  1510.         killsp();
  1511.       }
  1512.     }else{
  1513.       /*  auch anderes erlaubt    */
  1514.       first=mymalloc(CLS);
  1515.       first->next=first->other=0;
  1516.       first->tree=tree;
  1517.       killsp();
  1518.     }
  1519.   }
  1520.   if(bracket){
  1521.     if(*s==','){s++;killsp();}
  1522.     if(*s=='}'){s++;killsp();} else error(128);
  1523.   }
  1524.   return(first);
  1525. }
  1526.  
  1527.  
  1528.  
  1529.